package com.tommy.ddp
{
	import com.tommy.ddp.box.DdpBox;
	import com.tommy.ddp.grid.DdpGrid;
	
	import org.taomee.ds.HashMap;

	public class GridDataManager
	{
		private static var gridHashMap:HashMap;
		private static var boxHashMap:HashMap;
		
		public function GridDataManager()
		{
		}
		setup();
		private static function setup():void
		{
			gridHashMap = new HashMap();
			boxHashMap = new HashMap();
		}
		public static function destroy():void
		{
			var arr:Array = gridHashMap.getValues();
			for each(var i:DdpGrid in arr)
			{
				i.dispose();
			}
			gridHashMap.clear();
			
			arr = boxHashMap.getValues();
			for each(var j:DdpBox in arr)
			{
				j.dispose();
			}
			boxHashMap.clear();
		}
		
		/**
		 * 注册格子索引
		 * @param xIndex
		 * @param yIndex
		 * @param grid
		 * 
		 */		
		public static function regGrid(xIndex:int,yIndex:int,grid:DdpGrid):void
		{
			gridHashMap.add(xIndex+"_"+yIndex,grid);
		}
		/**
		 * 获取格子
		 * @param xIndex
		 * @param yIndex
		 * @return 
		 * 
		 */		
		public static function getGrid(xIndex:int,yIndex:int):DdpGrid
		{
			return gridHashMap.getValue(xIndex+"_"+yIndex);
		}
		
		/**
		 * 注册棋子索引
		 * @param xIndex
		 * @param yIndex
		 * @param box
		 * 
		 */		
		public static function regBox(xIndex:int,yIndex:int,box:DdpBox):void
		{
			boxHashMap.add(xIndex+"_"+yIndex,box);
		}
		/**
		 * 获取棋子
		 * @param xIndex
		 * @param yIndex
		 * @return 
		 * 
		 */		
		public static function getBox(xIndex:int,yIndex:int):DdpBox
		{
			return boxHashMap.getValue(xIndex+"_"+yIndex);
		}
		/**
		 * 获取所有棋子
		 * @return 
		 * 
		 */		
		public static function get allBox():Array
		{
			return boxHashMap.getValues();
		}
		
		/**
		 * BOX被消除以后从HASHMAP里移除
		 * @param box
		 * 
		 */		
		public static function removeBox(box:DdpBox):void
		{
			boxHashMap.remove(box.column+"_"+box.row);
		}
		
		/**
		 * 某个棋子消除的时候，自身以及周围格子的处理
		 * @param box
		 * 
		 */		
		public static function disposeGridofBoxPos(box:DdpBox):void
		{
			var grid:DdpGrid = getGrid(box.column,box.row);
			if(grid)
				grid.clear();
		}
		
		
		public static function punchGrids(vect:Vector.<DdpGrid>):void
		{
			for each(var i:DdpGrid in vect)
			{
				i.punch();
			}
		}
	}
}